package utils;

import java.util.ArrayList;
import java.util.List;

import data.Carrier;
import data.CarrierType;
import data.Package;

public class IterationalCarrierUtils {

	CarrierUtils utils = new CarrierUtils();

	public List<Carrier> pushPackagesOnCarriers(List<Package> packagesForFile,
			List<CarrierType> carrierTypesForFile) {
		List<Carrier> currentCarriers = new ArrayList<Carrier>();
		for (Package p : packagesForFile) {
			if (carrierTypesForFile.size() == 0) {
				createNewCarrierForPackage(p, currentCarriers,
						carrierTypesForFile);
			} else {
				boolean foundPotentialCarrier = false;
				Carrier optimalCarrier = null;

				for (Carrier c : currentCarriers) {
					float weight = c.getWeight() + p.getWeight();
					float capacity = c.getCapacity() + p.getCapacity();
					float capMax = (float) (c.getType().getFieldBase() * c
							.getType().getMaxHeight());
					if ((weight <= c.getType().getMaxWeight())
							&& (capMax >= capacity)) {
						CarrierType potentialType = utils
								.getPotentialCarrierType(carrierTypesForFile,
										p.getPrefferedCarrier());
						capMax = (float) (potentialType.getFieldBase() * potentialType
								.getMaxHeight());
						if ((weight <= potentialType.getMaxWeight())
								&& (capMax >= capacity)) {
							foundPotentialCarrier = true;
							if (optimalCarrier != null) {
								// if (optimalCarrier.getType().getFieldBase()
								// >= c
								// .getType().getFieldBase()) {
								optimalCarrier = c;
								// }
							} else {
								optimalCarrier = c;
							}

						}
					}
				}
				if (foundPotentialCarrier) {
					optimalCarrier.pushPackage(p);
					utils.updateCarrierTypeIfNecessary(optimalCarrier,
							carrierTypesForFile);
				} else {
					createNewCarrierForPackage(p, currentCarriers,
							carrierTypesForFile);
				}
			}
		}
		return currentCarriers;
	}

	/**
	 * @param p
	 * @param carrierTypesForFile
	 */
	public void createNewCarrierForPackage(Package p,
			List<Carrier> currentCarriers, List<CarrierType> carrierTypesForFile) {
		for (CarrierType ct : carrierTypesForFile) {
			// TU SIE MOZNA ZASTANOWIC
			if (ct.getName().equals(p.getPrefferedCarrier())) {
				Carrier c = new Carrier(ct);
				c.pushPackage(p);
				currentCarriers.add(c);
				break;
			}
		}
	}

	public List<List<Carrier>> intersectBestResult(
			List<List<Carrier>> carriers, List<List<Carrier>> carriers2,
			int length) {
		List<List<Carrier>> result = new ArrayList<List<Carrier>>();
		for (int i = 0; i < length; i++) {
			float fieldBaseSum = 0, minCapacitySum = 0, fieldBaseSum2 = 0, minCapacitySum2 = 0;
			for (Carrier c : carriers.get(i)) {
				fieldBaseSum += c.getType().getFieldBase();
				minCapacitySum += c.getCapacity();
			}
			for (Carrier c : carriers2.get(i)) {
				fieldBaseSum2 += c.getType().getFieldBase();
				minCapacitySum2 += c.getCapacity();
			}
			if (fieldBaseSum >= fieldBaseSum2) {
				if (fieldBaseSum == fieldBaseSum2) {
					if (minCapacitySum > minCapacitySum2) {
						result.add(carriers2.get(i));
					} else {
						result.add(carriers.get(i));
					}
				} else {
					result.add(carriers2.get(i));
				}
			} else {
				result.add(carriers.get(i));
			}
		}
		return result;
	}
}
